package xiao_jin.fantastic_journey.core.common.item;

import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.Rarity;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;

import java.util.List;
import java.util.Random;

public class Diamond extends Item {
    public Diamond() {
        super(new Item.Settings().maxCount(1).rarity(Rarity.EPIC));
    }

    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
        if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
            var exception = new RuntimeException(exceptionTexts.get(random.nextInt(exceptionTexts.size())));
            exception.setStackTrace(new StackTraceElement[0]);
            throw exception;
        }
        return TypedActionResult.pass(player.getStackInHand(hand));
    }

    @Override
    public Text getName(ItemStack stack) {
        return Text.of(this.getRandomItemName());
    }

    @Override
    public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
        super.appendTooltip(stack, context, tooltip, type);
        tooltip.add(Text.of(String.format(Text.translatable("item.fantastic_journey.use_diamond").getString(), (random.nextInt(64) + 1), this.getRandomItemName())));
    }

    /* Dirty Method for Client */
    private static final List<String> exceptionTexts = List.of(
            "请拿稳您的物品",
            "卡车 丢失",
            "光光吃掉了你的游戏！",
            "对立吃掉了你的游戏！",
            "Potential -0.01",
            "99% Terminal Forgotten",
            "我去，红（Kou）！",
            "对他使用反射吧！"
    );
    private static final Random random = new Random();
    private static final List<Item> items = Registries.ITEM.stream().toList();
    private static final char[] code = "@#$%^&".toCharArray();

    private static int tickOfRender = 0;
    private static final int maxTickOfRender = 20 * 6;

    private static String textHolder = "";
    private static final StringBuilder textInRender = new StringBuilder();

    private String getRandomItemName() {
        if (textHolder.isEmpty()) textHolder = items.get(random.nextInt(items.size())).getName().getString();

        if (textInRender.length() < textInRender.length() || !textInRender.toString().equals(textHolder)) {
            if (++tickOfRender < 5) return textInRender.toString();
            tickOfRender = 0;
            if (!textInRender.isEmpty()) {
                var pointer = textInRender.length() - 1;
                textInRender.deleteCharAt(pointer);
                textInRender.append(textHolder.charAt(pointer));
                if (textInRender.length() == textHolder.length()) return textInRender.toString();
            }
            textInRender.append(code[random.nextInt(code.length)]);
            return textInRender.toString();
        } else if (++tickOfRender < maxTickOfRender) return textInRender.toString();

        tickOfRender = 0;
        textHolder = "";
        textInRender.setLength(0);

        return "NaN";
    }
}
